//
// Created by 17775 on 2024/11/1.
//

#include "LiveMode_Analysis.h"
#include "../RayCi/LiveMode.h"

void LiveMode_Analysis::getData() {
    _isGettingNewResultData.store(true, std::memory_order_release);
    m_resultThread = std::thread(&LiveMode_Analysis::fetchData, this);
}

void LiveMode_Analysis::stopGetData() {
    _isGettingNewResultData.store(false, std::memory_order_release);
    if(m_resultThread.joinable()) {
        m_resultThread.join();
    }
}

//std::map<std::string, DataVariant> LiveMode_Analysis::fetchData() {
//    std::map<std::string, DataVariant> dataMap;
//    while(_isGettingNewResultData.load(std::memory_order_acquire)) {
//        dataMap["Minimum"] = getStatisticMinimum();
//        dataMap["MinimumUnit"] = getStatisticMinimumUnit();
//        dataMap["Peak"] = getStatisticPeak();
//        dataMap["PeakUnit"] = getStatisticPeakUnit();
//        emit getNewResult(dataMap);
//        std::this_thread::sleep_for(std::chrono::milliseconds(10));
//    }
//    return dataMap;
//}

void LiveMode_Analysis::fetchData() {
    while(_isGettingNewResultData.load(std::memory_order_acquire)) {
        switch (methodsType) {
            case MethodsType::None: {
                StatisticResult statisticResult = getStatistic();
                emit getNewStatistic(statisticResult);
                break;
            }
            case MethodsType::Threshold: {
                StatisticResult statisticResult = getStatistic();
                ThresholdResult thresholdResult = getThresholdResult();
                emit getNewThreshold(statisticResult, thresholdResult);
                break;
            }
            case MethodsType::_2ndMoments: {
                StatisticResult statisticResult = getStatistic();
                SecondMomentsResult secondMomentsResult = getSecondMomentResult();
                emit getNewSecondMoments(statisticResult, secondMomentsResult);
                break;
            }
            case MethodsType::FitGauss: {
                StatisticResult statisticResult = getStatistic();
                GaussResult gaussResult = getGaussResult();
                emit getNewGauss(statisticResult, gaussResult);
                break;
            }
            case MethodsType::FitSuperGauss: {
                StatisticResult statisticResult = getStatistic();
                GaussResult gaussResult = getSuperGaussResult();
                emit getNewSuperGauss(statisticResult, gaussResult);
                break;
            }
            case MethodsType::Plateau: {
                StatisticResult statisticResult = getStatistic();
                PlateauResult plateauResult = getPlateauResult();
                emit getNewPlateau(statisticResult, plateauResult);
                break;
            }
            case MethodsType::SimpleGeometry: {
                StatisticResult statisticResult = getStatistic();
                SimpleGeometry simpleGeometry = getSimpleGeometryResult();
                emit getNewSimpleGeometry(statisticResult, simpleGeometry);
                break;
            }
            case MethodsType::KnifeEdge: {
                StatisticResult statisticResult = getStatistic();
                KnifeEdge knifeEdge = getKnifeEdgeResult();
                emit getNewKnifeEdge(statisticResult, knifeEdge);
                break;
            }
            case MethodsType::MovingSlit: {
                StatisticResult statisticResult = getStatistic();
                KnifeEdge knifeEdge = getMovingSlitResult();
                emit getNewMovingSlit(statisticResult, knifeEdge);
                break;
            }
            case MethodsType::Dropdown: {
                StatisticResult statisticResult = getStatistic();
                KnifeEdge knifeEdge = getDropdownResult();
                emit getNewDropdown(statisticResult, knifeEdge);
                break;
            }
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

double LiveMode_Analysis::getCombinedStatisticMinimum(int32_t nColumn, bool bOnlyIlluminated) {
    bool bValid = false;
    RayCi_LiveMode_Analysis_CombinedStatistic_Minimum_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bValid);
    double dMinimum = 0;
    if(bValid) {
        RayCi_LiveMode_Analysis_CombinedStatistic_Minimum_getMinimum(_nDocIdLive, nColumn, bOnlyIlluminated, &dMinimum);
    }
    return dMinimum;
}

QString LiveMode_Analysis::getCombinedStatisticMinimumUnit() {
    char szUnit_buffer[10];
    int nSize = sizeof(szUnit_buffer);
    RayCi_LiveMode_Analysis_CombinedStatistic_Minimum_getUnitA(_nDocIdLive, szUnit_buffer, &nSize);
    return QString::fromLatin1(szUnit_buffer);
}

double LiveMode_Analysis::getCombinedStatisticPeak(int32_t nColumn, bool bOnlyIlluminated) {
    bool bValid = false;
    RayCi_LiveMode_Analysis_CombinedStatistic_Peak_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bValid);
    double dPeak = 0;
    if(bValid) {
        RayCi_LiveMode_Analysis_CombinedStatistic_Peak_getPeak(_nDocIdLive, nColumn, bOnlyIlluminated, &dPeak);
    }
    return dPeak;
}

QString LiveMode_Analysis::getCombinedStatisticPeakUnit() {
    char szUnit_buffer[10];
    int nSize = sizeof(szUnit_buffer);
    RayCi_LiveMode_Analysis_CombinedStatistic_Peak_getUnitA(_nDocIdLive, szUnit_buffer, &nSize);
    return QString::fromLatin1(szUnit_buffer);
}

double LiveMode_Analysis::getCombinedStatisticPeakPos_X(int32_t nColumn, bool bOnlyIlluminated) {
    bool bValid = false;
    RayCi_LiveMode_Analysis_CombinedStatistic_PeakPos_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bValid);
    double dPeakPos_X = 0;
    if(bValid) {
        RayCi_LiveMode_Analysis_CombinedStatistic_PeakPos_getX(_nDocIdLive, nColumn, bOnlyIlluminated, &dPeakPos_X);
    }
    return dPeakPos_X;
}

double LiveMode_Analysis::getCombinedStatisticPeakPos_Y(int32_t nColumn, bool bOnlyIlluminated) {
    bool bValid = false;
    RayCi_LiveMode_Analysis_CombinedStatistic_PeakPos_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bValid);
    double dPeakPos_Y = 0;
    if(bValid) {
        RayCi_LiveMode_Analysis_CombinedStatistic_PeakPos_getY(_nDocIdLive, nColumn, bOnlyIlluminated, &dPeakPos_Y);
    }
    return dPeakPos_Y;
}

QString LiveMode_Analysis::getCombinedStatisticPeakPosUnit() {
    char szUnit_buffer[10];
    int nSize = sizeof(szUnit_buffer);
    RayCi_LiveMode_Analysis_CombinedStatistic_PeakPos_getUnitA(_nDocIdLive, szUnit_buffer, &nSize);
    return QString::fromLatin1(szUnit_buffer);
}

double LiveMode_Analysis::getCombinedStatisticAverage(int32_t nColumn, bool bOnlyIlluminated) {
    bool bValid = false;
    RayCi_LiveMode_Analysis_CombinedStatistic_Average_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bValid);
    double dAverage = 0;
    if(bValid) {
        RayCi_LiveMode_Analysis_CombinedStatistic_Average_getAverage(_nDocIdLive, nColumn, bOnlyIlluminated, &dAverage);
    }
    return dAverage;
}

QString LiveMode_Analysis::getCombinedStatisticAverageUnit() {
    char szUnit_buffer[10];
    int nSize = sizeof(szUnit_buffer);
    RayCi_LiveMode_Analysis_CombinedStatistic_Average_getUnitA(_nDocIdLive, szUnit_buffer, &nSize);
    return QString::fromLatin1(szUnit_buffer);
}

double LiveMode_Analysis::getCombinedStatisticRMS(int32_t nColumn, bool bOnlyIlluminated) {
    bool bValid = false;
    RayCi_LiveMode_Analysis_CombinedStatistic_RMS_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bValid);
    double dRMS = 0;
    if(bValid) {
        RayCi_LiveMode_Analysis_CombinedStatistic_RMS_getRMS(_nDocIdLive, nColumn, bOnlyIlluminated, &dRMS);
    }
    return dRMS;
}

QString LiveMode_Analysis::getCombinedStatisticRMSUnit() {
    char szUnit_buffer[10];
    int nSize = sizeof(szUnit_buffer);
    RayCi_LiveMode_Analysis_CombinedStatistic_RMS_getUnitA(_nDocIdLive, szUnit_buffer, &nSize);
    return QString::fromLatin1(szUnit_buffer);
}

double LiveMode_Analysis::getCombinedStatisticStDev(int32_t nColumn, bool bOnlyIlluminated) {
    bool bValid = false;
    RayCi_LiveMode_Analysis_CombinedStatistic_StDev_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bValid);
    double dStDev = 0;
    if(bValid) {
        RayCi_LiveMode_Analysis_CombinedStatistic_StDev_getStDev(_nDocIdLive, nColumn, bOnlyIlluminated, &dStDev);
    }
    return dStDev;
}

QString LiveMode_Analysis::getCombinedStatisticStDevUnit() {
    char szUnit_buffer[10];
    int nSize = sizeof(szUnit_buffer);
    RayCi_LiveMode_Analysis_CombinedStatistic_StDev_getUnitA(_nDocIdLive, szUnit_buffer, &nSize);
    return QString::fromLatin1(szUnit_buffer);
}

double LiveMode_Analysis::getCombinedStatisticPower(int nColumn, bool bOnlyIlluminated) {
    double dPower = 0;
    RayCi_LiveMode_Analysis_CombinedStatistic_Power_getPower(_nDocIdLive, nColumn, bOnlyIlluminated, &dPower);
    return dPower;
}

bool LiveMode_Analysis::combinedStatisticPowerIsValid(int nColumn, bool bOnlyIlluminated) {
    bool bValid = false;
    RayCi_LiveMode_Analysis_CombinedStatistic_Power_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bValid);
    return bValid;
}

QString LiveMode_Analysis::getCombinedStatisticPowerUnit() {
    char szUnit_buffer[10];
    int nSize = sizeof(szUnit_buffer);
    RayCi_LiveMode_Analysis_CombinedStatistic_Power_getUnitA(_nDocIdLive, szUnit_buffer, &nSize);
    return QString::fromLatin1(szUnit_buffer);
}

// Statistic
StatisticResult LiveMode_Analysis::getStatistic(int32_t nColumn, bool bOnlyIlluminated) {
    StatisticResult statisticResult;
    bool bNumberOfPixelValid = false;
    bool bPowerValid = false;
    bool bPeakValid = false;
    bool bPeakPosValid = false;
    bool bSNRValid = false;
    RayCi_LiveMode_Analysis_Statistic_NumberOfPixel_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bNumberOfPixelValid);
    RayCi_LiveMode_Analysis_Statistic_Power_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bPowerValid);
    RayCi_LiveMode_Analysis_Statistic_Peak_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bPeakValid);
    RayCi_LiveMode_Analysis_Statistic_PeakPos_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bPeakPosValid);
    RayCi_LiveMode_Analysis_Statistic_SNR_isValid(_nDocIdLive, nColumn, &bSNRValid);
    if(bNumberOfPixelValid) {
        RayCi_LiveMode_Analysis_Statistic_NumberOfPixel_getNumber(_nDocIdLive, nColumn, bOnlyIlluminated, &statisticResult.dNumber);
    }
    if(bPowerValid) {
        RayCi_LiveMode_Analysis_Statistic_Power_getPower(_nDocIdLive, nColumn, bOnlyIlluminated, &statisticResult.dPower);
    }
    if(bPeakValid) {
        RayCi_LiveMode_Analysis_Statistic_Peak_getPeak(_nDocIdLive, nColumn, bOnlyIlluminated, &statisticResult.dPeak);
    }
    if(bPeakPosValid) {
        RayCi_LiveMode_Analysis_Statistic_PeakPos_getX(_nDocIdLive, nColumn, bOnlyIlluminated, &statisticResult.dPosX);
        RayCi_LiveMode_Analysis_Statistic_PeakPos_getX(_nDocIdLive, nColumn, bOnlyIlluminated, &statisticResult.dPosY);
    }
    if(bSNRValid) {
        RayCi_LiveMode_Analysis_Statistic_SNR_getSNR(_nDocIdLive, nColumn, &statisticResult.dSNR);
    }
    return statisticResult;
}
QString LiveMode_Analysis::getStatisticPowerUnit(int32_t nColumn, bool bOnlyIlluminated) {
    char szUnit_buffer[10];
    int nSize = sizeof(szUnit_buffer);
    RayCi_LiveMode_Analysis_Statistic_Power_getUnitA(_nDocIdLive, szUnit_buffer, &nSize);
    return QString::fromLatin1(szUnit_buffer);
}
double LiveMode_Analysis::getStatisticMinimum(int32_t nColumn, bool bOnlyIlluminated) {
    bool bValid = false;
    RayCi_LiveMode_Analysis_Statistic_Minimum_isValid(_nDocIdLive, nColumn, bOnlyIlluminated, &bValid);
    double dMinimum = 0;
    if(bValid) {
        RayCi_LiveMode_Analysis_Statistic_Minimum_getMinimum(_nDocIdLive, nColumn, bOnlyIlluminated, &dMinimum);
    }
    return dMinimum;
}
QString LiveMode_Analysis::getStatisticMinimumUnit() {
    char szUnit_buffer[10];
    int nSize = sizeof(szUnit_buffer);
    RayCi_LiveMode_Analysis_Statistic_Minimum_getUnitA(_nDocIdLive, szUnit_buffer, &nSize);
    return QString::fromLatin1(szUnit_buffer);
}
QString LiveMode_Analysis::getStatisticPeakUnit() {
    char szUnit_buffer[10];
    int nSize = sizeof(szUnit_buffer);
    RayCi_LiveMode_Analysis_Statistic_Peak_getUnitA(_nDocIdLive, szUnit_buffer, &nSize);
    return QString::fromLatin1(szUnit_buffer);
}
QString LiveMode_Analysis::getStatisticPeakPosUnit() {
    char szUnit_buffer[10];
    int nSize = sizeof(szUnit_buffer);
    RayCi_LiveMode_Analysis_Statistic_PeakPos_getUnitA(_nDocIdLive, szUnit_buffer, &nSize);
    return QString::fromLatin1(szUnit_buffer);
}

// Threshold
ThresholdResult LiveMode_Analysis::getThresholdResult(int32_t nColumn) {
    ThresholdResult thresholdResult;
    bool bEffectivePowerValid = false;
    bool bPowerRatioValid = false;
    bool bEffectiveAreaValid = false;
    bool bMeanIntensityValid = false;
    bool bFlatnessValid = false;
    bool bUniformityValid = false;
    bool bEdgeSteepnessValid = false;
    RayCi_LiveMode_Analysis_Threshold_EffectivePower_isValid(_nDocIdLive, nColumn, &bEffectivePowerValid);
    RayCi_LiveMode_Analysis_Threshold_PowerRatio_isValid(_nDocIdLive, nColumn, &bPowerRatioValid);
    RayCi_LiveMode_Analysis_Threshold_EffectiveArea_isValid(_nDocIdLive, nColumn, &bEffectiveAreaValid);
    RayCi_LiveMode_Analysis_Threshold_MeanIntensity_isValid(_nDocIdLive, nColumn, &bMeanIntensityValid);
    RayCi_LiveMode_Analysis_Threshold_Flatness_isValid(_nDocIdLive, nColumn, &bFlatnessValid);
    RayCi_LiveMode_Analysis_Threshold_Uniformity_isValid(_nDocIdLive, nColumn, &bUniformityValid);
    RayCi_LiveMode_Analysis_Threshold_EdgeSteepness_isValid(_nDocIdLive, nColumn, &bEdgeSteepnessValid);
    if(bEffectivePowerValid) {
        RayCi_LiveMode_Analysis_Threshold_EffectivePower_getPower(_nDocIdLive, nColumn, &thresholdResult.dEffectivePower);
    }
    if(bPowerRatioValid) {
        RayCi_LiveMode_Analysis_Threshold_PowerRatio_getRatio(_nDocIdLive, nColumn, &thresholdResult.dPowerRatio);
    }
    if(bEffectiveAreaValid) {
        RayCi_LiveMode_Analysis_Threshold_EffectiveArea_getArea(_nDocIdLive, nColumn, &thresholdResult.dEffectiveArea);
    }
    if(bMeanIntensityValid) {
        RayCi_LiveMode_Analysis_Threshold_MeanIntensity_getIntensity(_nDocIdLive, nColumn, &thresholdResult.dMeanIntensity);
    }
    if(bFlatnessValid) {
        RayCi_LiveMode_Analysis_Threshold_Flatness_getFlatness(_nDocIdLive, nColumn, &thresholdResult.dFlatness);
    }
    if(bUniformityValid) {
        RayCi_LiveMode_Analysis_Threshold_Uniformity_getUniformity(_nDocIdLive, nColumn, &thresholdResult.dUniformity);
    }
    if(bEdgeSteepnessValid) {
        RayCi_LiveMode_Analysis_Threshold_EdgeSteepness_getSteepness(_nDocIdLive, nColumn, &thresholdResult.dEdgeSteepness);
    }
    return thresholdResult;
}

// SecondMoments
SecondMomentsResult LiveMode_Analysis::getSecondMomentResult(int32_t nColumn) {
    SecondMomentsResult secondMomentsResult;
    bool bCentroidValid = false;
    bool bWidthLabValid = false;
    bool bWidthBeamValid = false;
    bool bCrossSectAreaValid = false;
    bool bFractionValid = false;
    RayCi_LiveMode_Analysis_SecondMoments_Centroid_isValid(_nDocIdLive, nColumn, &bCentroidValid);
    RayCi_LiveMode_Analysis_SecondMoments_WidthLab_isValid(_nDocIdLive, nColumn, &bWidthLabValid);
    RayCi_LiveMode_Analysis_SecondMoments_WidthBeam_isValid(_nDocIdLive, nColumn, &bWidthBeamValid);
    RayCi_LiveMode_Analysis_SecondMoments_CrossSectArea_isValid(_nDocIdLive, nColumn, &bCrossSectAreaValid);
//    RayCi_LiveMode_Analysis_Settings_isFractionAvailable(_nDocIdLive, &bFractionValid);
    RayCi_LiveMode_Analysis_SecondMoments_Intensity_isValid(_nDocIdLive, nColumn, &bFractionValid);
    if(bCentroidValid) {
        RayCi_LiveMode_Analysis_SecondMoments_Centroid_getX(_nDocIdLive, nColumn, &secondMomentsResult.dCentroidX);
        RayCi_LiveMode_Analysis_SecondMoments_Centroid_getY(_nDocIdLive, nColumn, &secondMomentsResult.dCentroidY);
    }
    if(bWidthLabValid) {
        RayCi_LiveMode_Analysis_SecondMoments_WidthLab_getX(_nDocIdLive, nColumn, &secondMomentsResult.dWidthLabX);
        RayCi_LiveMode_Analysis_SecondMoments_WidthLab_getY(_nDocIdLive, nColumn, &secondMomentsResult.dWidthLabY);
    }
    if(bWidthBeamValid) {
        RayCi_LiveMode_Analysis_SecondMoments_WidthBeam_getMajor(_nDocIdLive, nColumn, &secondMomentsResult.dMajor);
        RayCi_LiveMode_Analysis_SecondMoments_WidthBeam_getMinor(_nDocIdLive, nColumn, &secondMomentsResult.dMinor);
        RayCi_LiveMode_Analysis_SecondMoments_WidthBeam_getAzimuthAngle(_nDocIdLive, nColumn, &secondMomentsResult.dAzimuthAngle);
        RayCi_LiveMode_Analysis_SecondMoments_WidthBeam_getEllipticity(_nDocIdLive, nColumn, &secondMomentsResult.dEllipticity);
        RayCi_LiveMode_Analysis_SecondMoments_WidthBeam_getEccentricity(_nDocIdLive, nColumn, &secondMomentsResult.dEccentricity);
    }
    if(bCrossSectAreaValid) {
        RayCi_LiveMode_Analysis_SecondMoments_CrossSectArea_getArea(_nDocIdLive, nColumn, &secondMomentsResult.dArea);
    }
    if(bFractionValid) {
//        RayCi_LiveMode_Analysis_Settings_getFraction(_nDocIdLive, &secondMomentsResult.dPowerFraction);
        RayCi_LiveMode_Analysis_SecondMoments_Intensity_getIntensity(_nDocIdLive, nColumn, &secondMomentsResult.dPowerFraction);
    }
    return secondMomentsResult;
}

//Fit Gauss
GaussResult LiveMode_Analysis::getGaussResult(int32_t nColumn) {
    bool bCentroidValid = false;
    bool bWidthBeamValid = false;
    bool bIntensityValid = false;
    bool bGFIValid = false;
    bool bCrossSectAreaValid = false;
    bool bWidthLabValid = false;
    bool bCorrelation = false;
    RayCi_LiveMode_Analysis_Gauss_Centroid_isValid(_nDocIdLive, nColumn, &bCentroidValid);
    RayCi_LiveMode_Analysis_Gauss_WidthLab_isValid(_nDocIdLive, nColumn, &bWidthLabValid);
    RayCi_LiveMode_Analysis_Gauss_WidthBeam_isValid(_nDocIdLive, nColumn, &bWidthBeamValid);
    RayCi_LiveMode_Analysis_Gauss_CrossSectArea_isValid(_nDocIdLive, nColumn, &bCrossSectAreaValid);
    RayCi_LiveMode_Analysis_Gauss_Intensity_isValid(_nDocIdLive, nColumn, &bIntensityValid);
    RayCi_LiveMode_Analysis_Gauss_GFI_isValid(_nDocIdLive, nColumn, &bGFIValid);
    RayCi_LiveMode_Analysis_Gauss_Correlation_isValid(_nDocIdLive, nColumn, &bCorrelation);
    GaussResult gaussResult;
    if(bCentroidValid) {
        RayCi_LiveMode_Analysis_Gauss_Centroid_getX(_nDocIdLive, nColumn, &gaussResult.dCentroidX);
        RayCi_LiveMode_Analysis_Gauss_Centroid_getY(_nDocIdLive, nColumn, &gaussResult.dCentroidY);
    }
    if(bWidthLabValid) {
        RayCi_LiveMode_Analysis_Gauss_WidthLab_getX(_nDocIdLive, nColumn, &gaussResult.dWidthLabX);
        RayCi_LiveMode_Analysis_Gauss_WidthLab_getY(_nDocIdLive, nColumn, &gaussResult.dWidthLabY);
    }
    if(bWidthBeamValid) {
        RayCi_LiveMode_Analysis_Gauss_WidthBeam_getMajor(_nDocIdLive, nColumn, &gaussResult.dMajor);
        RayCi_LiveMode_Analysis_Gauss_WidthBeam_getMinor(_nDocIdLive, nColumn, &gaussResult.dMinor);
        RayCi_LiveMode_Analysis_Gauss_WidthBeam_getAzimuthAngle(_nDocIdLive, nColumn, &gaussResult.dAzimuthAngle);
        RayCi_LiveMode_Analysis_Gauss_WidthBeam_getEllipticity(_nDocIdLive, nColumn, &gaussResult.dEllipticity);
        RayCi_LiveMode_Analysis_Gauss_WidthBeam_getEccentricity(_nDocIdLive, nColumn, &gaussResult.dEccentricity);
    }
    if(bCrossSectAreaValid) {
        RayCi_LiveMode_Analysis_Gauss_CrossSectArea_getArea(_nDocIdLive, nColumn, &gaussResult.dArea);
    }
    if(bIntensityValid) {
        RayCi_LiveMode_Analysis_Gauss_Intensity_getIntensity(_nDocIdLive, nColumn, &gaussResult.dPowerFraction);
    }
    if(bGFIValid) {
        RayCi_LiveMode_Analysis_Gauss_GFI_getGFI(_nDocIdLive, nColumn, &gaussResult.dGFI);
    }
    if(bCorrelation) {
        RayCi_LiveMode_Analysis_Gauss_Correlation_getCorrelation(_nDocIdLive, nColumn, &gaussResult.dCorrelation);
    }
    return gaussResult;
}

// Fit SuperGauss
GaussResult LiveMode_Analysis::getSuperGaussResult(int32_t nColumn) {
    bool bCentroidValid = false;
    bool bWidthBeamValid = false;
    bool bIntensityValid = false;
    bool bGFIValid = false;
    bool bCrossSectAreaValid = false;
    bool bWidthLabValid = false;
    bool bCorrelation = false;
    RayCi_LiveMode_Analysis_SuperGaussEllipse_Centroid_isValid(_nDocIdLive, nColumn, &bCentroidValid);
    RayCi_LiveMode_Analysis_SuperGaussEllipse_WidthLab_isValid(_nDocIdLive, nColumn, &bWidthLabValid);
    RayCi_LiveMode_Analysis_SuperGaussEllipse_WidthBeam_isValid(_nDocIdLive, nColumn, &bWidthBeamValid);
    RayCi_LiveMode_Analysis_SuperGaussEllipse_CrossSectArea_isValid(_nDocIdLive, nColumn, &bCrossSectAreaValid);
    RayCi_LiveMode_Analysis_Gauss_Intensity_isValid(_nDocIdLive, nColumn, &bIntensityValid);
    RayCi_LiveMode_Analysis_SuperGaussEllipse_GFI_isValid(_nDocIdLive, nColumn, &bGFIValid);
    RayCi_LiveMode_Analysis_SuperGaussEllipse_Correlation_isValid(_nDocIdLive, nColumn, &bCorrelation);
    GaussResult gaussResult;
    if(bCentroidValid) {
        RayCi_LiveMode_Analysis_SuperGaussEllipse_Centroid_getX(_nDocIdLive, nColumn, &gaussResult.dCentroidX);
        RayCi_LiveMode_Analysis_SuperGaussEllipse_Centroid_getY(_nDocIdLive, nColumn, &gaussResult.dCentroidY);
    }
    if(bWidthLabValid) {
        RayCi_LiveMode_Analysis_SuperGaussEllipse_WidthLab_getX(_nDocIdLive, nColumn, &gaussResult.dWidthLabX);
        RayCi_LiveMode_Analysis_SuperGaussEllipse_WidthLab_getY(_nDocIdLive, nColumn, &gaussResult.dWidthLabY);
    }
    if(bWidthBeamValid) {
        RayCi_LiveMode_Analysis_SuperGaussEllipse_WidthBeam_getMajor(_nDocIdLive, nColumn, &gaussResult.dMajor);
        RayCi_LiveMode_Analysis_SuperGaussEllipse_WidthBeam_getMinor(_nDocIdLive, nColumn, &gaussResult.dMinor);
        RayCi_LiveMode_Analysis_SuperGaussEllipse_WidthBeam_getAzimuthAngle(_nDocIdLive, nColumn, &gaussResult.dAzimuthAngle);
        RayCi_LiveMode_Analysis_SuperGaussEllipse_WidthBeam_getEllipticity(_nDocIdLive, nColumn, &gaussResult.dEllipticity);
        RayCi_LiveMode_Analysis_SuperGaussEllipse_WidthBeam_getEccentricity(_nDocIdLive, nColumn, &gaussResult.dEccentricity);
    }
    if(bCrossSectAreaValid) {
        RayCi_LiveMode_Analysis_SuperGaussEllipse_CrossSectArea_getArea(_nDocIdLive, nColumn, &gaussResult.dArea);
    }
    if(bIntensityValid) {
        RayCi_LiveMode_Analysis_SuperGaussEllipse_Intensity_getIntensity(_nDocIdLive, nColumn, &gaussResult.dPowerFraction);
    }
    if(bGFIValid) {
        RayCi_LiveMode_Analysis_SuperGaussEllipse_GFI_getGFI(_nDocIdLive, nColumn, &gaussResult.dGFI);
    }
    if(bCorrelation) {
        RayCi_LiveMode_Analysis_SuperGaussEllipse_Correlation_getCorrelation(_nDocIdLive, nColumn, &gaussResult.dCorrelation);
    }
    return gaussResult;
}

// Plateau
PlateauResult LiveMode_Analysis::getPlateauResult(int32_t nColumn) {
    PlateauResult plateauResult;
    bool bCentroidValid = false;
    bool bWidthBeamValid = false;
    bool bCrossSectAreaValid = false;
    bool bUniformityValid = false;
    bool bRoughnessValid = false;
    bool bPowerFractionValid = false;
    bool bGFIValid = false;
    bool bCorrelationValid = false;
    bool bTopHatFactorValid = false;
    RayCi_LiveMode_Analysis_Plateau_Centroid_isValid(_nDocIdLive, nColumn, &bCentroidValid);
    RayCi_LiveMode_Analysis_Plateau_WidthBeam_isValid(_nDocIdLive, nColumn, &bWidthBeamValid);
    RayCi_LiveMode_Analysis_Plateau_CrossSectArea_isValid(_nDocIdLive, nColumn, &bCrossSectAreaValid);
    RayCi_LiveMode_Analysis_Plateau_Uniformity_isValid(_nDocIdLive, nColumn, &bUniformityValid);
    RayCi_LiveMode_Analysis_Plateau_Roughness_isValid(_nDocIdLive, nColumn, &bRoughnessValid);
    RayCi_LiveMode_Analysis_Plateau_Intensity_isValid(_nDocIdLive, nColumn, &bPowerFractionValid);
    RayCi_LiveMode_Analysis_Plateau_GFI_isValid(_nDocIdLive, nColumn, &bGFIValid);
    RayCi_LiveMode_Analysis_Plateau_Correlation_isValid(_nDocIdLive, nColumn, &bCorrelationValid);
    RayCi_LiveMode_Analysis_Plateau_TopHatFactor_isValid(_nDocIdLive, nColumn, &bTopHatFactorValid);
    if(bCentroidValid) {
        RayCi_LiveMode_Analysis_Plateau_Centroid_getX(_nDocIdLive, nColumn, &plateauResult.dCentroidX);
        RayCi_LiveMode_Analysis_Plateau_Centroid_getY(_nDocIdLive, nColumn, &plateauResult.dCentroidY);
    }
    if(bWidthBeamValid) {
        RayCi_LiveMode_Analysis_Plateau_WidthBeam_getMajor(_nDocIdLive, nColumn, &plateauResult.dMajor);
        RayCi_LiveMode_Analysis_Plateau_WidthBeam_getMinor(_nDocIdLive, nColumn, &plateauResult.dMinor);
        RayCi_LiveMode_Analysis_Plateau_WidthBeam_getAzimuthAngle(_nDocIdLive, nColumn, &plateauResult.dAzimuthAngle);
        RayCi_LiveMode_Analysis_Plateau_WidthBeam_getEllipticity(_nDocIdLive, nColumn, &plateauResult.dEllipticity);
        RayCi_LiveMode_Analysis_Plateau_WidthBeam_getEccentricity(_nDocIdLive, nColumn, &plateauResult.dEccentricity);
    }
    if(bCrossSectAreaValid) {
        RayCi_LiveMode_Analysis_Plateau_CrossSectArea_getArea(_nDocIdLive, nColumn, &plateauResult.dArea);
    }
    if(bUniformityValid) {
        RayCi_LiveMode_Analysis_Plateau_Uniformity_getUniformity(_nDocIdLive, nColumn, &plateauResult.dUniformity);
    }
    if(bRoughnessValid) {
        RayCi_LiveMode_Analysis_Plateau_Roughness_getRoughness(_nDocIdLive, nColumn, &plateauResult.dRoughness);
    }
    if(bPowerFractionValid) {
        RayCi_LiveMode_Analysis_Plateau_Intensity_getIntensity(_nDocIdLive, nColumn, &plateauResult.dPowerFraction);
    }
    if(bGFIValid) {
        RayCi_LiveMode_Analysis_Plateau_GFI_getGFI(_nDocIdLive, nColumn, &plateauResult.dGFI);
    }
    if(bCorrelationValid) {
        RayCi_LiveMode_Analysis_Plateau_Correlation_getCorrelation(_nDocIdLive, nColumn, &plateauResult.dCorrelation);
    }
    if(bTopHatFactorValid) {
        RayCi_LiveMode_Analysis_Plateau_TopHatFactor_getFactor(_nDocIdLive, nColumn, &plateauResult.dTopHatFactor);
    }
    return plateauResult;
}

// SimpleGeometry
SimpleGeometry LiveMode_Analysis::getSimpleGeometryResult(int32_t nColumn) {
    SimpleGeometry simpleGeometry;
    bool bCentroidValid = false;
    bool bWidthBeamValid = false;
    bool bCrossSectAreaValid = false;
    bool bPowerFractionValid = false;
    RayCi_LiveMode_Analysis_SimpleGeometry_Centroid_isValid(_nDocIdLive, nColumn, &bCentroidValid);
    RayCi_LiveMode_Analysis_SimpleGeometry_WidthBeam_isValid(_nDocIdLive, nColumn, &bWidthBeamValid);
    RayCi_LiveMode_Analysis_SimpleGeometry_CrossSectArea_isValid(_nDocIdLive, nColumn, &bCrossSectAreaValid);
    RayCi_LiveMode_Analysis_SimpleGeometry_Intensity_isValid(_nDocIdLive, nColumn, &bPowerFractionValid);
    if(bCentroidValid) {
        RayCi_LiveMode_Analysis_SimpleGeometry_Centroid_getX(_nDocIdLive, nColumn, &simpleGeometry.dCentroidX);
        RayCi_LiveMode_Analysis_SimpleGeometry_Centroid_getY(_nDocIdLive, nColumn, &simpleGeometry.dCentroidY);
    }
    if(bWidthBeamValid) {
        RayCi_LiveMode_Analysis_SimpleGeometry_WidthBeam_getMajor(_nDocIdLive, nColumn, &simpleGeometry.dMajor);
        RayCi_LiveMode_Analysis_SimpleGeometry_WidthBeam_getMinor(_nDocIdLive, nColumn, &simpleGeometry.dMinor);
        RayCi_LiveMode_Analysis_SimpleGeometry_WidthBeam_getAzimuthAngle(_nDocIdLive, nColumn, &simpleGeometry.dAzimuthAngle);
        RayCi_LiveMode_Analysis_SimpleGeometry_WidthBeam_getEllipticity(_nDocIdLive, nColumn, &simpleGeometry.dEllipticity);
        RayCi_LiveMode_Analysis_SimpleGeometry_WidthBeam_getEccentricity(_nDocIdLive, nColumn, &simpleGeometry.dEccentricity);
    }
    if(bCrossSectAreaValid) {
        RayCi_LiveMode_Analysis_SimpleGeometry_CrossSectArea_getArea(_nDocIdLive, nColumn, &simpleGeometry.dArea);
    }
    if(bPowerFractionValid) {
        RayCi_LiveMode_Analysis_SimpleGeometry_Intensity_getIntensity(_nDocIdLive, nColumn, &simpleGeometry.dPowerFraction);
    }
    return simpleGeometry;
}

KnifeEdge LiveMode_Analysis::getKnifeEdgeResult(int32_t nColumn) {
    KnifeEdge knifeEdge;
    bool bWidthBeamValid = false;
    RayCi_LiveMode_Analysis_KnifeEdge_WidthBeam_isValid(_nDocIdLive, nColumn, &bWidthBeamValid);
    if(bWidthBeamValid) {
        RayCi_LiveMode_Analysis_KnifeEdge_WidthBeam_getMajor(_nDocIdLive, nColumn, &knifeEdge.dMajor);
        RayCi_LiveMode_Analysis_KnifeEdge_WidthBeam_getMinor(_nDocIdLive, nColumn, &knifeEdge.dMinor);
        RayCi_LiveMode_Analysis_KnifeEdge_WidthBeam_getAzimuthAngle(_nDocIdLive, nColumn, &knifeEdge.dAzimuthAngle);
        RayCi_LiveMode_Analysis_KnifeEdge_WidthBeam_getEllipticity(_nDocIdLive, nColumn, &knifeEdge.dEllipticity);
        RayCi_LiveMode_Analysis_KnifeEdge_WidthBeam_getEccentricity(_nDocIdLive, nColumn, &knifeEdge.dEccentricity);
    }
    return knifeEdge;
}

// MovingSlit
KnifeEdge LiveMode_Analysis::getMovingSlitResult(int32_t nColumn) {
    KnifeEdge knifeEdge;
    bool bWidthBeamValid = false;
    RayCi_LiveMode_Analysis_MovingSlit_WidthBeam_isValid(_nDocIdLive, nColumn, &bWidthBeamValid);
    if(bWidthBeamValid) {
        RayCi_LiveMode_Analysis_MovingSlit_WidthBeam_getMajor(_nDocIdLive, nColumn, &knifeEdge.dMajor);
        RayCi_LiveMode_Analysis_MovingSlit_WidthBeam_getMinor(_nDocIdLive, nColumn, &knifeEdge.dMinor);
        RayCi_LiveMode_Analysis_MovingSlit_WidthBeam_getAzimuthAngle(_nDocIdLive, nColumn, &knifeEdge.dAzimuthAngle);
        RayCi_LiveMode_Analysis_MovingSlit_WidthBeam_getEllipticity(_nDocIdLive, nColumn, &knifeEdge.dEllipticity);
        RayCi_LiveMode_Analysis_MovingSlit_WidthBeam_getEccentricity(_nDocIdLive, nColumn, &knifeEdge.dEccentricity);
    }
    return knifeEdge;
}

// Dropdown
KnifeEdge LiveMode_Analysis::getDropdownResult(int32_t nColumn) {
    KnifeEdge knifeEdge;
    bool bWidthBeamValid = false;
    RayCi_LiveMode_Analysis_Dropdown_WidthBeam_isValid(_nDocIdLive, nColumn, &bWidthBeamValid);
    if(bWidthBeamValid) {
        RayCi_LiveMode_Analysis_Dropdown_WidthBeam_getMajor(_nDocIdLive, nColumn, &knifeEdge.dMajor);
        RayCi_LiveMode_Analysis_Dropdown_WidthBeam_getMinor(_nDocIdLive, nColumn, &knifeEdge.dMinor);
        RayCi_LiveMode_Analysis_Dropdown_WidthBeam_getAzimuthAngle(_nDocIdLive, nColumn, &knifeEdge.dAzimuthAngle);
        RayCi_LiveMode_Analysis_Dropdown_WidthBeam_getEllipticity(_nDocIdLive, nColumn, &knifeEdge.dEllipticity);
        RayCi_LiveMode_Analysis_Dropdown_WidthBeam_getEccentricity(_nDocIdLive, nColumn, &knifeEdge.dEccentricity);
    }
    return knifeEdge;
}

LiveMode_Analysis::~LiveMode_Analysis() {
    _isGettingNewResultData.store(false, std::memory_order_release);
    if(m_resultThread.joinable()) {
        m_resultThread.join();
    }
}















